package org.flycloud.web.platform.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

public class MessageSender {

	private String cityCode;
	private String unitCode;
	private String password;

	public String getCityCode() {
		return cityCode;
	}

	public void setCityCode(String cityCode) {
		this.cityCode = cityCode;
	}

	public String getUnitCode() {
		return unitCode;
	}

	public void setUnitCode(String unitCode) {
		this.unitCode = unitCode;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean send(String phone, String content) {
		try {
			content = URLEncoder.encode(content, "GBK");
			String url = " http://123.233.245.237:8080/ema/person/SendSms?EAccount="
					+ this.cityCode
					+ "&PAccount="
					+ this.unitCode
					+ "&Password="
					+ this.password
					+ "&Phone="
					+ phone
					+ "&Content=" + content + "&SubCode=99901&ProgramID=1";

//			String result = "";
			BufferedReader in = null;
			try {
				URL realUrl = new URL(url);
				// 打开和URL之间的连接
				URLConnection conn = realUrl.openConnection();
				// 设置通用的请求属性
				conn.setRequestProperty("accept", "*/*");
				conn.setRequestProperty("connection", "Keep-Alive");
				conn.setRequestProperty("user-agent",
						"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
				// 建立实际的连接
				conn.connect();
				// 获取所有响应头字段
				Map<String, List<String>> map = conn.getHeaderFields();
				// 遍历所有的响应头字段
				for (String key : map.keySet()) {
					System.out.println(key + "--->" + map.get(key));
				}
				// 定义BufferedReader输入流来读取URL的响应
				in = new BufferedReader(new InputStreamReader(
						conn.getInputStream()));
				while (in.readLine() != null) {
					
				}
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
			// 使用finally块来关闭输入流
			finally {
				try {
					if (in != null) {
						in.close();
					}
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		}
	}

	public static void main(String[] args) {
		MessageSender ms = new MessageSender();
		ms.setCityCode("3706");
		ms.setUnitCode("370609");
		ms.setPassword("123456");
		//ms.send("13583541635", "你好");
		ms.send("18615941518", "你好");
	}
}
